package com.zq.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zq.product.dao.dto.ProductSkuDto;
import com.zq.product.dao.entity.Product;
import com.zq.product.dao.entity.ProductDetails;
import com.zq.product.dao.entity.ProductSku;
import com.zq.product.dao.vo.ProductItemVo;
import com.zq.product.mapper.ProductDetailsMapper;
import com.zq.product.mapper.ProductMapper;
import com.zq.product.mapper.ProductSkuMapper;
import com.zq.product.service.IProductService;
import com.zq.product.service.IProductSkuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品sku 服务实现类
 * </p>
 *
 * @author 张乔
 * @since 2025-05-13
 */
@Service
@Slf4j
public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, ProductSku> implements IProductSkuService {

    @Resource
    private ProductSkuMapper productSkuMapper;

    //畅销商品，前10个
    @Override
    @Cacheable(value ="product", key = "'ten'")
    public List<ProductSku> boomProductTen() {
        List<ProductSku> productSkuList=   productSkuMapper.boomProductTen();
        log.info("畅销商品，前10个："+productSkuList);
        return productSkuList;
    }

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductDetailsMapper productDetailsMapper;

    //根据skuId获取商品的详情
    @Override
    public ProductItemVo detail(Long id) {
        ProductSku productSku = productSkuMapper.selectById(id);
        Long productId = productSku.getProductId();
        Product product = productMapper.selectById(productId);

        List<ProductSku> skuList = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, productId));
        //建立sku规格与skuId对应关系
        Map<String,Object> skuSpecValueMap = new HashMap<>();

        skuList.forEach(it-> {
            skuSpecValueMap.put(it.getSkuSpec(),it.getId());
        });

        ProductDetails productDetails = productDetailsMapper.selectOne(new LambdaQueryWrapper<ProductDetails>()
                .eq(ProductDetails::getProductId, productId));
        ProductItemVo productItemVo=new ProductItemVo();

        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));

        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);

        return productItemVo;


    }

    //    获取所有商品
    @Override
    @Cacheable(value ="product", key = "'list'")
    public List<ProductSku> getProductList() {

        List<ProductSku> productSkuList=    productSkuMapper.getProductList();


        return productSkuList;
    }

    //分页查询商品
    @Override
    public PageInfo<ProductSku> getProductListByPage(Integer page, Integer limit) {

        PageHelper.startPage(page,limit);
        List<ProductSku> productSkuList=    productSkuMapper.getProductList();
        PageInfo<ProductSku> pageInfo = new PageInfo<>(productSkuList);
        return pageInfo;
    }

    //    根据商品名称查询商品
    @Override
    public List<ProductSku> getProductListBy(ProductSkuDto productSkuDto) {
        log.info("根据商品名称查询商品："+productSkuDto);
     List<ProductSku> productSkuList=   productSkuMapper.getProductListBy(productSkuDto);

        return productSkuList;
    }


}
